package com.controller;


import com.model.Array;
import com.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;

@RequestMapping("/mvc")
@RestController
//@Controller
public class testMvc {
    @RequestMapping("array")
    public String Array(String[] arr) {
        System.out.println("mvc");
        return Arrays.toString(arr);
    }

    @RequestMapping("t1")
    // 定义一个名为t1的方法，接收一个Array类型的参数a
    public String t1(@RequestBody Array a) {
        // 打印参数a
        System.out.println(a);
        // 返回参数a的字符串表示
        return a.toString();
    }

    @RequestMapping("/t2")
    public String t2(int a) {
        System.out.println(a);
        return "t2";
    }

    @RequestMapping("/t3/{id}/{name}")
    // 定义一个处理请求的方法，路径为/t3/{id}/{name}，其中{id}和{name}为路径变量
    public String t3(@PathVariable int id, @PathVariable("name") String name) {
        // 使用@PathVariable注解获取路径变量id和name的值
        System.out.println(id + name);
        // 打印id和name的值
        return "id：" + id + "name：" + name;
        // 返回一个字符串，包含id和name的值
    }

    @RequestMapping("t4")
    public String t4(@RequestPart MultipartFile file) throws IOException {
        String name = file.getOriginalFilename();
        file.transferTo(new File("D:\\图片" + name));
        return name;
    }

    @RequestMapping("t5")
    public String t5(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        StringBuffer buffer = new StringBuffer();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                buffer.append("cookieName：" + cookie.getName() + "cookieValue：" + cookie.getValue());
            }
        }
        System.out.println(buffer.toString());
        return "返回Cookie信息" + buffer.toString();
    }

    @RequestMapping("/m10")
    public String method10(HttpServletRequest request, HttpServletResponse response) {
// 获取所有 cookie 信息
        Cookie[] cookies = request.getCookies();
//打印Cookie信息
        StringBuilder builder = new StringBuilder();
        if (cookies != null) {
            for (Cookie ck : cookies) {
                builder.append(ck.getName() + ":" + ck.getValue());
            }
        }
        return "Cookie信息:" + builder;
    }


    @RequestMapping("/m11")
    public String m11(@CookieValue("name") String name, @CookieValue("age") String age) {
        return "name:" + name + "  age:" + age;
    }

    @RequestMapping("setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session != null) {
            session.setAttribute("name", "zhangsan");
        }
        return "设置session成功";
    }


    @RequestMapping("/session")
    public String setSession(HttpSession session) {
        session.setAttribute("name", "zhangsan");
        return "设置session成功";
    }

    @RequestMapping("/se")
    public String setsession(@SessionAttribute(value = "name", required = false) String name) {
        return "获取session中的那么属性：" + name;
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request, HttpServletResponse response) {
String userAgent=request.getHeader("User-Agent");
        System.out.println(userAgent);
        return userAgent;
    }

    @RequestMapping("/getuseragent")
    public String getUserAgent(@RequestHeader("User-Agent")String userAgent){
        return userAgent;
    }


@RequestMapping("/t6")
    public Object t6(){
        return "/index.html";

}

@RequestMapping("/returnJson")
    public HashMap<String,String> returnJSON(){
        HashMap<String,String>ret=new HashMap<>();
        ret.put("1","asdad");
        ret.put("2","asdad");
        ret.put("3","asdad");
        ret.put("4","asdad");
        return ret;
}

@RequestMapping(value = "returnJson2",produces = "application/json")
public String returnJson2(){
        return "{\"sucess\":true}";
        }


    @RequestMapping(value = "returnJson2",produces = "application/json; charset = UTF-8")
    public String returnJson3(){
        return "{\"sucess\":true}";
    }

@RequestMapping("/returnCSS")
    public Object returnCSS(){
        return "/test.css";
}
@RequestMapping("/returnJS")
    public Object returnJS(){
        return "/test.js";
}

@RequestMapping("setHeader")
    public String setHeader(HttpServletResponse response){
        response.setHeader("name","zhangsan");
        return "设置响应头成功";
    }

    @Resource(name = "admin")
    public User user;

    @RequestMapping("/getUser")
    public String getUser(){
        return "name:"+user.getName()+"    age:"+user.getAge();
    }




}
